package com.blog.controller;

import com.blog.check.result.Result;
import com.blog.form.PictureForm;
import com.blog.service.service.PictureService;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 图控制器
 *
 * @author 泡面ing
 * @date 2021/12/08
 */
@Controller
@RequestMapping("/picture")
public class PictureController {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    private PictureService pictureService;

    @Autowired
    public void setPictureService(PictureService pictureService){
        this.pictureService = pictureService;
    }
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Result list(HttpServletResponse response){
        Result result;
        try {
            result = pictureService.list(null);

        } catch (Exception e) {
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常：" + e.getMessage());
            logger.error("查询信息异常：", e);
        }
        return result;
    }
    @RequestMapping(value = "/{picture_id}",method = RequestMethod.GET)
    @ResponseBody
    public Result get(@PathVariable("picture_id") String pictureId, HttpServletResponse response){
        Result result;
        try {
            result = pictureService.get(pictureId);

        } catch (Exception e) {
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常：" + e.getMessage());
            logger.error("查询信息异常：", e);
        }
        return result;
    }
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestBody PictureForm pictureForm, HttpServletResponse response){
        Result result;
        try {
            result = pictureService.save(pictureForm);

        } catch (Exception e) {
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常：" + e.getMessage());
            logger.error("查询信息异常：", e);
        }
        return result;
    }
    @RequestMapping(value = "/{picture_id}",method = RequestMethod.PUT)
    @ResponseBody
    public Result update(@PathVariable("picture_id") String pictureId,@RequestBody PictureForm pictureForm, HttpServletResponse response){
        Result result;
        try {
            result = pictureService.update(pictureId,pictureForm);

        } catch (Exception e) {
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常：" + e.getMessage());
            logger.error("查询信息异常：", e);
        }
        return result;
    }
    @RequestMapping(value = "/{picture_id}",method = RequestMethod.DELETE)
    @ResponseBody
    public Result remove(@PathVariable("picture_id") String pictureId,HttpServletResponse response){
        Result result;
        try {
            result = pictureService.remove(pictureId);

        } catch (Exception e) {
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常：" + e.getMessage());
            logger.error("查询信息异常：", e);
        }
        return result;
    }

}
